package com.legrand.www.module.authentication.service.impl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.legrand.www.module.authentication.dto.TokenDTO;
import com.legrand.www.module.authentication.mapper.UserMapper;
import com.legrand.www.module.common.util.EncryptionUtil;
import com.legrand.www.module.core.entity.User;
import com.legrand.www.module.authentication.service.UserService;
import com.legrand.www.module.core.util.JwtToken;
import com.legrand.www.module.exception.exception.CustomizeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author xiah
 * date: 2018/6/26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

       /*@Override
       public User getUser() {
              *//*使用mybatis-plus分页*//*
              EntityWrapper wrapper = new EntityWrapper();
              wrapper.where("id>{0}", 0);
              *//*从第一条记录开始   不包括第一条   最多返回三条记录*//*
              RowBounds rowBounds = new RowBounds(1, 3);
              List list = userMapperer.selectPage(rowBounds, wrapper);

               *//*使用自定义分页 当前页 每页多少条*//*
              Page page = new Page(1, 3);
              List list2 = userMapperer.getList(page);
              User user = userMapperer.selectById(1L);
              return user;
       }*/

    @Override
    public User getUser() {
        userMapper.getClass();
        return null;
    }

    /**
     * @author xiah
     * 2018-06-27 04:01:08
     * 验证账号和密码是否正确 密码正确返回对应的token
     */
    @Override
    public TokenDTO checkUser(String userName, String password) throws Exception {
        User user = new User();
        user.setUserName(userName);
        User u = this.userMapper.selectOne(user);
        /**用户密码正确*/
        if (u != null && u.getPassword().equals(EncryptionUtil.md5(password))) {
            /**缺少加密方式*/
            final String accessToken = JwtToken.createToken();
            final String refreshToken = JwtToken.createPermanentToken();
            return new TokenDTO(accessToken, refreshToken, "MD5");
        }
        throw new CustomizeException("501", "userName or password is wrong!");
    }
}
